# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=cvxopt
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.3.2
pkgrel=1
pkgdesc="Convex optimization based on the Python programming language (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://cvxopt.org/'
license=('spdx:GPL-3.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools-scm"
             "${MINGW_PACKAGE_PREFIX}-python-wheel"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-dsdp"
         "${MINGW_PACKAGE_PREFIX}-glpk")
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname/-/_}-${pkgver}.tar.gz")
sha256sums=('3461fa42c1b2240ba4da1d985ca73503914157fc4c77417327ed6d7d85acdbe6')

prepare() {
  cd "${srcdir}"
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  CVXOPT_BLAS_LIB='openblas'                          \
  CVXOPT_LAPACK_LIB='openblas'                        \
  CVXOPT_BUILD_GSL=1                                  \
  CVXOPT_BUILD_FFTW=1                                 \
  CVXOPT_BUILD_DSDP=1                                 \
  CVXOPT_BUILD_GLPK=1                                 \
  CVXOPT_GSL_LIB_DIR=${MINGW_PREFIX}/lib              \
  CVXOPT_GSL_INC_DIR=${MINGW_PREFIX}/include          \
  CVXOPT_FFTW_LIB_DIR=${MINGW_PREFIX}/lib             \
  CVXOPT_FFTW_INC_DIR=${MINGW_PREFIX}/include         \
  CVXOPT_GLPK_LIB_DIR=${MINGW_PREFIX}/lib             \
  CVXOPT_GLPK_INC_DIR=${MINGW_PREFIX}/include         \
  CVXOPT_DSDP_LIB_DIR=${MINGW_PREFIX}/lib             \
  CVXOPT_DSDP_INC_DIR=${MINGW_PREFIX}/include/dsdp    \
  CVXOPT_SUITESPARSE_LIB_DIR=${MINGW_PREFIX}/lib      \
  CVXOPT_SUITESPARSE_INC_DIR=${MINGW_PREFIX}/include  \
    ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m pytest
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"
}
